package com.tianyang.yukuaifuzs.business.auth.model;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.tianyang.yukuaifuzs.business.auth.bean.AreaCityOutput;
import com.tianyang.yukuaifuzs.business.auth.bean.BankRegOutput;
import com.tianyang.yukuaifuzs.business.auth.bean.BusinessOutput;
import com.tianyang.yukuaifuzs.business.auth.bean.IdCardOutput;
import com.tianyang.yukuaifuzs.business.auth.bean.LicenseRegOutput;
import com.tianyang.yukuaifuzs.business.auth.bean.MerchantVerifyOutput;
import com.tianyang.yukuaifuzs.business.auth.bean.SampleImageOutput;
import com.tianyang.yukuaifuzs.business.auth.contract.AuthConfig;
import com.tianyang.yukuaifuzs.business.login.bean.LoginOutput;
import com.tianyang.yukuaifuzs.business.main.bean.PosUserOutput;
import com.tianyang.yukuaifuzs.common.interfaces.CommonConfig;
import com.tianyang.yukuaifuzs.rxservice.client.PlateAgentClient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observable;

/**
 * Description：实名模块
 * Created by 薛勇军 on 2024/1/9.
 */
public class AuthModel {

    private static volatile AuthModel mInstance;

    private AuthModel() {
        if (mInstance != null) {
            throw new IllegalStateException("Already initialized.");
        }
    }

    public static AuthModel getInstance() {
        AuthModel result = mInstance;
        if (result == null) {
            synchronized (AuthModel.class) {
                result = mInstance;
                if (result == null) {
                    mInstance = result = new AuthModel();
                }
            }
        }
        return result;
    }

    /**
     * 注册
     */
    public Observable<LoginOutput> userRegister(String phone, String inviteCode, String posChannel) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("phone", phone);
        reqData.put("inviteCode", inviteCode);
        reqData.put("channel", posChannel);
        return PlateAgentClient.getInstance().userRegister(reqData);
    }

    /**
     * 服务商实名认证
     */
    public Observable<JSONObject> sendIdCertByService(Map<String, String> map, String userName) {
        if (map == null) map = new HashMap<>();
        map.put("tel", userName);
        map.put("type", "1");
        map.put("merchantType", "2");
        map.put("accBankType", "2");
        return PlateAgentClient.getInstance().sendIdCertByService(map);
    }

    /**
     * 修改商户类型
     */
    public Observable<JSONObject> saveIdCertTemp(String tel, String merchantType) {
        Map<String, String> map = new HashMap<>();
        map.put("tel", tel);
        map.put("merchantType", merchantType);
        return PlateAgentClient.getInstance().saveIdCertTemp(map);
    }

    /**
     * 小微保存进件临时数据
     */
    public Observable<JSONObject> saveIdCertTemp(Map<String, String> map, String userName, String mchType) {
        if (map == null) map = new HashMap<>();
        map.put("tel", userName);
        map.put("type", "0");
        map.put("merchantType", mchType);
        map.put("businessType", "1");
        map.put("mchType", mchType);//0   企业   1  小微   2 个体
        if (!map.containsKey("accBankType"))
            map.put("accBankType", "2");//账户类型  0对公   2对私
        return PlateAgentClient.getInstance().saveIdCertTemp(map);
    }

    /**
     * 小微实名认证
     */
    public Observable<JSONObject> sendIdCert(Map<String, String> map, String userName, String mchType, String posStatus) {
        if (map == null) map = new HashMap<>();
        map.put("tel", userName);
        map.put("type", "0");
        map.put("merchantType", mchType);
        map.put("businessType", "1");
        map.put("mchType", mchType);//0   企业   1  小微   2 个体
        if (!map.containsKey("accBankType"))
            map.put("accBankType", "2");//账户类型  0对公   2对私
        if (TextUtils.equals("0", posStatus))
            map.put("registerType", "1");
        else
            map.put("registerType", posStatus);
        return PlateAgentClient.getInstance().sendIdCert(map);
    }

    public Observable<JSONObject> sendIdCert(Map<String, String> map) {
        return PlateAgentClient.getInstance().sendIdCert(map);
    }

    /**
     * 查询小微进件临时数据
     */
    public Observable<JSONObject> getIdCertTemp(String userName) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("tel", userName);
        return PlateAgentClient.getInstance().getIdCertTemp(reqData);
    }

    public Observable<List<BusinessOutput>> getMcc(int pageNum, String keyword, String area, String merchantType, String mccType, String channel) {
        if (TextUtils.equals(AuthConfig.MCC_TYPE_BUSINESS, mccType)) {
            return posMccCode(pageNum, keyword, merchantType, channel);
        } else if (TextUtils.equals(AuthConfig.MCC_TYPE_BANK, mccType)) {
            return posBankBranchCode(pageNum, keyword, area);
        } else {
            return yplMccCode(pageNum, keyword, merchantType, mccType);
        }
    }

    /**
     * 查询行业MCC   所属行业
     */
    public Observable<List<BusinessOutput>> posMccCode(int pageNum, String keyword, String merchantType, String channel) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("merchantNo", "T201908211005");
        reqData.put("pageSize", CommonConfig.PAGE_SIZE_2 + "");
        reqData.put("pageNum", pageNum + "");
        if (TextUtils.equals(AuthConfig.MCH_TYPE_SMALL, merchantType)) {
            reqData.put("mchType", "1");
        }
        reqData.put("businessName", keyword);
        reqData.put("channel", channel);
        return PlateAgentClient.getInstance().posMccCode(reqData);
    }

    /**
     * 查询MCC  支付宝 微信
     */
    public Observable<List<BusinessOutput>> yplMccCode(int pageNum, String keyword, String merchantType, String mccType) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("merchantNo", "T201908211005");
        reqData.put("pageSize", CommonConfig.PAGE_SIZE_2 + "");
        reqData.put("pageNum", pageNum + "");
        if (TextUtils.equals(AuthConfig.MCH_TYPE_SMALL, merchantType)) {
            reqData.put("mchType", "1");
        }
        if (TextUtils.equals(AuthConfig.MCC_TYPE_ALI, mccType)) {
            reqData.put("type", "0");
        } else if (TextUtils.equals(AuthConfig.MCC_TYPE_WX, mccType)) {
            reqData.put("type", "1");
            if (TextUtils.equals(AuthConfig.MCH_TYPE_COMPANY, merchantType)) {
                reqData.put("merchantType", "企业");
            } else {
                reqData.put("merchantType", "个体工商户");
            }
        }
        reqData.put("classTypeThree", keyword);
        return PlateAgentClient.getInstance().yplMccCode(reqData);
    }

    /**
     * 查询银行支行
     */
    public Observable<List<BusinessOutput>> posBankBranchCode(int pageNum, String keyword, String area) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("merchantNo", "T201908211005");
        reqData.put("pageSize", CommonConfig.PAGE_SIZE_2 + "");
        reqData.put("pageNum", pageNum + "");
        reqData.put("bankName", keyword);
        reqData.put("city", area);
        return PlateAgentClient.getInstance().posBankBranchCode(reqData);
    }

    /**
     * 身份正面ORC识别
     */
    public Observable<IdCardOutput> idCardReg(String url) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("url", url);
        return PlateAgentClient.getInstance().idCardReg(reqData);
    }

    /**
     * 银行卡ORC
     */
    public Observable<BankRegOutput> bankReg(String url) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("url", url);
        return PlateAgentClient.getInstance().bankReg(reqData);
    }

    /**
     * 营业执照识别
     */
    public Observable<LicenseRegOutput> licenseReg(String url) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("url", url);
        return PlateAgentClient.getInstance().licenseReg(reqData);
    }

    /**
     * 省份
     */
    public Observable<List<AreaCityOutput>> getProvinceList() {
        return PlateAgentClient.getInstance().getProvinceList(new HashMap<>());
    }

    /**
     * 市
     */
    public Observable<List<AreaCityOutput>> getCityList(String provinceCode) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("provinceCode", provinceCode);
        return PlateAgentClient.getInstance().getCityList(reqData);
    }

    /**
     * 地区
     */
    public Observable<List<AreaCityOutput>> getAreaList(String provinceCode, String cityCode) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("provinceCode", provinceCode);
        reqData.put("cityCode", cityCode);
        return PlateAgentClient.getInstance().getAreaList(reqData);
    }

    public Observable<List<PosUserOutput>> posUserManage(int pageNum, String phone, String status) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("pageSize", CommonConfig.PAGE_SIZE + "");
        reqData.put("pageNum", pageNum + "");
        reqData.put("status", status);
        if (!TextUtils.isEmpty(phone))
            reqData.put("phone", phone);
        return PlateAgentClient.getInstance().posUserManage(reqData);
    }

    public Observable<JSONObject> getIdcertconfirm(String phone, int type) {
        Map<String, Object> reqData = new HashMap<>();
        reqData.put("phone", phone);
        reqData.put("type", type);
        return PlateAgentClient.getInstance().getIdcertconfirm(reqData);
    }

    public Observable<MerchantVerifyOutput> yplMerchantVerify(String phone) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("phone", phone);
        return PlateAgentClient.getInstance().yplMerchantVerify(reqData);
    }

    public Observable<SampleImageOutput> getSampleImage() {
        return PlateAgentClient.getInstance().getSampleImage();
    }

    public Observable<List<String>> getBusinessScopeList() {
        return PlateAgentClient.getInstance().getBusinessScopeList();
    }
}
